<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="java.util.Vector"%>
<%@ page import="com.dd.*"%>
<%@ page import="com.dd.exception.*"%>
<%@ page import="java.util.*" %>

<%@ include file="global.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%
	boolean errorUsername = false;
	boolean errorRealname = false;
	boolean errorNoUserDept = false;
	boolean errorNoPassword = false;
	boolean errorNoConfirmPassword = false;
	boolean errorPasswordsNotEqual = false;
	// error variables from user creation
	boolean errorUserAlreadyExists = false;
	boolean errorNoPermissionToCreate = false;
	
	boolean errorNotSelecteXiangMu = false;

	// overall error variable
	boolean errors = false;
	
	// creation success variable:
	boolean success = false;
	
	//debug 用的 做为入口
	UserManager tempuser = new UserManager();
	tempuser.getUserCount();
	
    // get parameters	
    String username         = ParamUtils.getParameter(request,"username");	
	String realname         = ParamUtils.getParameter(request,"realname");	
	String password         = ParamUtils.getParameter(request,"password");
	String confirmPassword  = ParamUtils.getParameter(request,"confirmPassword");
	int userBuMen		= ParamUtils.getIntParameter(request,"rdbumen",-1);
	//
	System.out.println("创建审核人员:");
	System.out.println(username);
	System.out.println(realname);
	System.out.println(password);
	System.out.println(confirmPassword);
	System.out.println(userBuMen);
	
	//审核人员所管项目
	XiangMu defaultXm = new XiangMu();
	ArrayList allXiangmus = defaultXm.GetAllXiangMusOld();
	int nXiangsCount = allXiangmus.size();
	
	Vector vsXiangmus = new Vector();
	for(int index = 0;index < nXiangsCount;index++)
	{
		XiangMu item = (XiangMu)(allXiangmus.get(index));
		String id = ((Integer)(new Integer(item.getId()))).toString();
		boolean bChecked = ParamUtils.getBooleanParameter(request,id);
		System.out.println(bChecked);
		if(bChecked)
		{
			vsXiangmus.add(new Integer(item.getId()));
		}
	}
	
	boolean doCreate        = ParamUtils.getBooleanParameter(request,"doCreate");
	System.out.println(doCreate);

	ArrayList allBuMens = BuMenInfo.GetAllBuMens();
	int nBuMensCount = allBuMens.size();
	
//	 trim up the passwords so no one can enter a password of spaces
	if( password != null ) {
		password = password.trim();
		if( password.equals("") ) { password = null; }
	}
	if( confirmPassword != null ) {
		confirmPassword = confirmPassword.trim();
		if( confirmPassword.equals("") ) { confirmPassword = null; }
	}
	

    // check for errors
	if( doCreate ) {

		if( username == null ) {
			errorUsername = true;
		}
		if( password == null ) {
			errorNoPassword = true;
		}
		if( confirmPassword == null ) {
			errorNoConfirmPassword = true;
		}
		if( password != null && confirmPassword != null
		    && !password.equals(confirmPassword) )
		{
			errorPasswordsNotEqual = true;
		}
		if ( userBuMen == -1 ){
		   errorNoUserDept = true;
		}
		int nSelectedXiangMuCount = vsXiangmus.size();
		if(nSelectedXiangMuCount <=0)
		{
			errorNotSelecteXiangMu = true;
		}
		errors = 	errorUsername 			|| errorNoPassword
		         || errorNoConfirmPassword 	|| errorPasswordsNotEqual
		         || errorNoUserDept 		|| errorNotSelecteXiangMu;
	}

    //UserManager userManager = null;
	if( !errors && doCreate ) {
		// get a user manager to edit user properties
		UserManager userManager = new UserManager();
		try {
			User newUser = userManager.createShenHeRenYuan(username,realname,password,userBuMen,vsXiangmus);
			success = true;
		}
		catch( UserAlreadyExistsException uaee ) {
			errorUserAlreadyExists = true;
			errorUsername = true;
			errors = true;
		}
	}

    // if a user was successfully created, say so and return (to stop the
	// jsp from executing
	if( success ) {
		response.sendRedirect("UserGeneralInfo.jsp");
		return;
	}
    


%>


<%@ include file="header.jsp" %>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>创建审核人员</title>
<script type="text/javascript">
    function chkallmessage(object)
			{			    
				var table = object.parentElement.parentElement.parentElement;
				var chkbox = table.getElementsByTagName("input");
				for ( var i = 0; i < chkbox.length; i ++ )
				{
				if (chkbox[i].getAttribute("type", 1) == "checkbox")
					{
					    chkbox[i].checked = object.checked; 
					}
				}				
			}
	</script>
</head>
<body>

<p>

<%	// print error messages
	if( !success && errors ) {
%>
	<p><font color="#ff0000" size="-1">
	<%	if( errorUserAlreadyExists ) { %>
		用户名 "<%= username %>" 已经存在。请使用其它的用户名。
	<%	} else if( errorPasswordsNotEqual ) { %>
		密码不一致。
	<%	} else if( errorNotSelecteXiangMu ) { %>
		没有选择项目。
	<%	} else { %>
		有错误发生。请检查以下字段并重试。
	<%	} %>
	</font><p>
<%	} %>

<p>

<h5 color="#FF0000" >创建审核人员</h5>
<hr/>

<form action="createShenHeUser.jsp" method="post" name="createForm"><input
	type="hidden" name="doCreate" value="true" /> <font size="-1"><b>新用户信息(以下各项都为必填内容)</b></font>
<p>
<table cellspacing="0" cellpadding="0" border="1" width="95%" align="right">
	<td>
	<table width="100%" height="81" border="0" cellpadding="3" cellspacing="1" bgcolor="<%= tblBorderColor %>">

		<%-- username --%>
		<tr bgcolor="#ffffff">
			<td  bgcolor="<%=tr_bakgroundColor%>" width = "40%" >用户名 <span id="Label6"
				style="color:Red;">*</span><br />
			</td>
			<td width = "60%" ><input type="text" name="username" size="30" /></td>
		</tr>

		<tr bgcolor="#ffffff">
			<td bgcolor="<%=tr_bakgroundColor%>">真实姓名 <span id="Label6"
				style="color:Red;">*</span>
			</td>
			<td><input type="text" name="realname" size="30" /></td>
		</tr>
		<%-- userDept --%>
		<tr bgcolor="#ffffff">
			<td bgcolor="<%=tr_bakgroundColor%>" style="height: 24px"><span id="Label13">所属项目部：</span> <span
				id="Label12" style="color:Red;">*</span></td>
			<td style="width: 361px; height: 24px">

			<div id="xiangmudiv" style="vertical-align:top">
			<table cellspacing="0" cellpadding="4" border="0" id="dgdxiangmu"
				style="color:#333333;width:158px;border-collapse:collapse;">
				<tr style="color:White;background-color:#5D7B9D;font-weight:bold;">
					<td><input id="cbkall" type="checkbox"
						onclick="chkallmessage(this)" /></td>
					<td>项目部名称</td>
				</tr>
				       <%
                    		if(nXiangsCount > 0)
                    		{
                    	 		Iterator it = allXiangmus.iterator();
                    	 		while(it.hasNext())
                    	 		{
                    	 			XiangMu xm = (XiangMu)it.next();
                    	 			if(xm != null)
                    	 			{
                    	 	
                 		 %>        

					<tr style="color:#333333;background-color:#F7F6F3;">
						<td>
							<input id="<%=xm.getId()%>" type="checkbox"	name="<%=xm.getId()%>" />
						</td>
						<td>
							<%=xm.getMingCheng() %>
						</td>
					</tr>
				
				       	<%
                    	 			}
                    	 		}
                    		}
                  		 %>  

			</table>
			&nbsp;</div>
			</td>
		</tr>
		<tr>
			<td bgcolor="<%=tr_bakgroundColor%>"><span id="Label4">帐户归属部门：</span> <span id="Label6"
				style="color:Red;">*</span></td>
			<td style="width: 361px">
			<select name="rdbumen" id="rdbumen" style="width:167px;">
			
			                  <%
                    				if(nBuMensCount > 0)
                    				{
                    	 				Iterator it = allBuMens.iterator();
                    	 				while(it.hasNext())
                    	 				{
                    	 					BuMenInfo bm = (BuMenInfo)it.next();
                    	 					if(bm != null)
                    	 					{
                    	 	
                 				  %>   
                   					  <option value="<%=bm.getId()%>"><%=bm.getBuMenMingCheng()%></option>   
      
                    			<%
                    	 					}
                    	 				}
                    				}
                  				  %>  

			</select> &nbsp;</td>
		</tr>
		<%-- password --%>
		<tr bgcolor="#ffffff">
			<td bgcolor="<%=tr_bakgroundColor%>">口令<span id="Label6"
				style="color:Red;">*</span></td>
			<td><input type="password" name="password" value="" size="20"
				maxlength="30"></td>
		</tr>

		<%-- confirm password --%>
		<tr bgcolor="#ffffff">
			<td bgcolor="<%=tr_bakgroundColor%>"> 请再次输入口令<span id="Label6"
				style="color:Red;">*</span></td>
			<td><input type="password" name="confirmPassword" value=""
				size="20" maxlength="30"></td>
		</tr>
		
		<td align="right">
			<input type="submit" value="创建用户" />
		</td>		
		<td>
			<input type="submit" value="放 弃" onclick="location.href='UserGeneralInfo.jsp';return false;" />
		</td>

	</table>
	</td>

</table>

<br clear="all">
<br>
</form>

<%@ include file="footer.jsp" %>
